<!DOCTYPE HTML><html lang="en">
<head>

<meta name="copyright" content="Copyright (c) IBM Corporation and others 2000, 2011. This page is made available under license. For full details see the LEGAL in the documentation book that contains this page." >

<meta charset="utf-8">
<link REL="STYLESHEET" HREF="../../book.css" TYPE="text/css">
<title>Message Bundles</title>
</head>

<body>
<h1>Message Bundles</h1>
<h2>Description </h2>
<p>Standard Java ResourceBundles have quite inefficient space characteristics.
  Since a running Eclipse tends to have many externalized messages, we have implemented
  a new message bundle system to be used in Eclipse. The mechanism is quite simple
  and completely generic - it can be used anywhere.</p>
<h3>Summary of the New Approach:</h3>
<ul>
  <li><code> messages.properties </code>- this file is same as before except all
    keys need to be valid Java identifiers.</li>
  <li> Each message file has a corresponding Java class.</li>
  <li> Each key/value pair in the file has a <code>public static String</code>
    field whose name is the same as the message key.</li>
  <li> When message bundles are loaded, the values of the fields are set to be
    the values from the <code>messages.properties </code>files.</li>
  <li> The message properties files are purged from memory. </li>
</ul>
<h3>When Creating a New Message:</h3>
<ul>
  <li> Create a field in your <code>Messages.java </code>file.</li>
  <li> Create a key/value pair in your <code>messages.properties </code>file where
    the key name matches the field name,</li>
  <li> To reference the message, simply reference the field (e.g. <code>Messages.my_key</code>)
    rather than the standard lookup.</li>
</ul>
<h2>Example Files:</h2>
<h3>Client Code</h3>
<h4>Old Code: </h4>
<pre><code>public class MyClass {
  public void myMethod() {
    String message;
    ...
    // no args
    message = Messages.getString(&quot;key.one&quot;); //$NON-NLS-1$
    ...
    // bind one arg
    message = MessageFormat.format(Messages.getString(&quot;key.two&quot;), new Object[] {&quot;example usage&quot;}); //$NON-NLS-1$ //$NON-NLS-2$
    ...
  }
}</code></pre>
<h4>New Code:</h4>
<pre><code>public class MyClass {
  public void myMethod() {
    String message;
    ...
    // no args
    message = Messages.key_one;
    ...
    // bind one arg
    message = NLS.bind(Messages.key_two, &quot;example usage&quot;); //$NON-NLS-1$
    ...
  }
}</code></pre>
<h3>Messages.java</h3>
<h4>Old Code:</h4>
<pre><code>public class Messages {
  private static final String BUNDLE_NAME = &quot;org.eclipse.core.utils.messages&quot;; //$NON-NLS-1$
  private static final ResourceBundle bundle = ResourceBundle.getBundle(BUNDLE_NAME);

  public static String getString(String key) {
    try {
      return bundle.getString(key);
    } catch (MissingResourceException e) {
      return key;
    }
  }
}</code></pre>
<h4>New Code:</h4>
<pre><code>import org.eclipse.osgi.util.NLS;</code></pre>
<pre><code>public class Messages extends NLS {
  private static final String BUNDLE_NAME = &quot;org.eclipse.core.utils.messages&quot;; //$NON-NLS-1$

  public static String key_one;
  public static String key_two;
  ...
  static {
    NLS.initializeMessages(BUNDLE_NAME, Messages.class);
  }
}</code></pre>
<h3>messages.properties</h3>
<h4>Old Code:</h4>
<pre>key.one = Hello world.
key.two = This is an {0} of binding with one argument.</pre>
<h4>New Code:</h4>
<pre>key_one = Hello world.
key_two = This is an {0} of binding with one argument.</pre>
</body>
</html>
